package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.model.CommoditySellingModel;
import com.morizhang.merchanmanager.api.plugins.BasicSaleManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;

@ServiceAnnotation(name = "SellingManageService")
public class SellingManageService implements IService {
    @Autowired
    BasicSaleManage basicSaleManage;

    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult selectAllCommoditySelling(JSONObject requestParams) {
        ApiResult result = new ApiResult();
        result.setData(basicSaleManage.selectAllCommoditySelling());
        return result;
    }

    public ApiResult updateCommoditySelling(JSONObject requestParams) {
        JSONObject data = requestParams.getJSONObject("data");
        CommoditySellingModel newCommoditySelling = JSON.toJavaObject(data, CommoditySellingModel.class);
        basicSaleManage.updateCommoditySelling(newCommoditySelling);
        return new ApiResult();
    }
}
